<?php
namespace app\common\audit\model;

use app\common\config\logic\CommonPickerLogic;
use app\common\config\logic\DictionaryLogic;
use app\common\config\model\ConfigCompany;
use app\common\order\model\Order;
use app\common\system\logic\UserLogic;
use think\Model;
use think\model\concern\SoftDelete;

class AuditResult extends Model
{
    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $type = [
        'source' => 'json',
        'process' => 'json',
    ];

    public function getTagAttr($value, $data)
    {
        $commonLogic = new CommonPickerLogic();
        return $commonLogic->getPickerItem('timing', $data['audit_type']);
    }

    public function getAuditStatusAttr($value, $data)
    {
        $map = [
            "待审核",
            "审核中",
            "审核通过",
            "审核驳回",
        ];
       $index = $data['status'];

       return isset($map[$index]) ? $map[$index] : '';
    }

    public function getCreatorAttr($value, $data) {
        $userLogic = new UserLogic();
        return $userLogic->getUser($data['create_user_id'], ['id', 'realname', 'status']);
    }

    public function getCompanyAttr($value, $data) {
        $dictionaryLogic = new DictionaryLogic();
        $row = $dictionaryLogic->getItem($data['company_id'], ConfigCompany::class, ['id', 'title', 'short_name']);

        return $row;
    }

    public function getOrderAttr($value, $data)
    {
        $orderModel = new Order();

        $fields = "id,order_type,category_id,company_id,cooperation_id,event_date,store_id,bridegroom";
        $order = $orderModel->field($fields)->where('id', $data['master_id'])->find();
        if (empty($order)) return [];

        $order->append(['category', 'company', 'cooperation', 'store', 'order_type_text']);

        return $order;
    }


}